<?php
/**
 * This file houses the XmlInputProtocol class.
 *
 * Copyright (c) 2010, Kalkomey Enterprises, Inc.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this 
 *   list of conditions and the following disclaimer in the documentation and/or other
 *   materials provided with the distribution.
 * - Neither the name of the Kalkomey Enterprises, Inc. nor the names of its 
 *   contributors may be used to endorse or promote products derived from this 
 *   software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @copyright Copyright (c) 2010, Kalkomey Enterprises, Inc.
 * @link http://code.google.com/p/swerv
 * @package Shared
 * @subpackage Protocols
 */

/**
 * The XmlInputProtocol class converts posted XML into a request object.
 *
 * @package Shared
 * @subpackage Protocols
 */
class XmlInputProtocol extends InputProtocol {
	
	/**
	 * A DOMDocument object for the posted XML.
	 *
	 * @var DOMDocument
	 */
	private $dom;
	
	/**
	 * The properties defined in the request object the XML will be converted to.
	 *
	 * @var array
	 */
	private $definedProperties;
	
	/**
	 * Reads the incoming post data and converts it to a request object.
	 *
	 * @uses InputProtocol::getRawPostData()
	 * @uses XmlInputProtocol::fillInChildNodes()
	 *
	 * @return Object
	 */
	public function getInput($requestClassName) {
		$data = $this->getRawPostData();
		if ($data == '') {
			$obj = new stdClass();
			return $obj;
		}
		$this->dom = new DOMDocument();
		$this->dom->loadXML($data);
		$requestObj = new $requestClassName();
		// set defined properties
		if (method_exists($requestObj, 'getProperties')) {
			$props = $requestObj->getProperties;
			$this->definedProperties = array_keys($props);
		} else {
			$props = get_object_vars($requestObj);
			$this->definedProperties = array_keys($props);
		}
		$this->fillInChildNodes($requestObj);
		return $requestObj;
	}
	
	/**
	 * Adds the additional properties to the request object.
	 *
	 * @uses XmlInputProtocol::getXmlAsArray()
	 * @uses InputProtocol::getArrayAsObject()
	 *
	 * @return void
	 */
	private function fillInChildNodes(&$requestObj) {
		$arr = $this->getXmlAsArray($this->dom);
		$propertiesArr = $arr[get_class($requestObj)];
		$this->getArrayAsObject($propertiesArr, $requestObj);
	}

	/**
	 * Converts the incoming XML into an array.
	 * Note: this is a recursive method
	 *
	 * @param DOMDocument &$node
	 * 
	 * @return array|string
	 */
	private function getXmlAsArray($node) {
		$occurance = array();
		if ($node->hasChildNodes()) {
			foreach ($node->childNodes as $child) {
				if (!array_key_exists($child->nodeName, $occurance)) {
					$occurance[$child->nodeName] = 0;
				}
				$occurance[$child->nodeName]++;
			}
		}
		if ($node->nodeType == XML_TEXT_NODE) { 
			$result = html_entity_decode(htmlentities($node->nodeValue, ENT_COMPAT, 'UTF-8'), ENT_COMPAT,'ISO-8859-15');
		} else {
			if ($node->hasChildNodes()) {
				$children = $node->childNodes;
				for ($i = 0; $i < $children->length; $i++) {
					$child = $children->item($i);
					if ($child->nodeName != '#text') {
						if ($occurance[$child->nodeName] > 1) {
							$result[$child->nodeName][] = $this->getXmlAsArray($child);
						} else {
							$result[$child->nodeName] = $this->getXmlAsArray($child);
						}
					} elseif ($child->nodeName == '#text') {
						$text = $this->getXmlAsArray($child);
						if (trim($text) != '') {
							$result = $text;
						}
					}
				}
			} 
			if($node->hasAttributes()) { 
				$attributes = $node->attributes;
				if(!is_null($attributes)) {
					foreach ($attributes as $key => $attr) {
						$result["@".$attr->name] = $attr->value;
					}
				}
			}
		}
		return $result;		
	}
	
}
